﻿
Public Class oGeneral
    Public Function GenerateUDT_UDF() As String
        Try
            'CreateUDT("GST
            Dim ret As String = ""
            ret = CreateUserCodeField()

            Return ret
        Catch ex As Exception
            Return ex.ToString
        End Try
    End Function
    Private Function CreateUserCodeField() As String
        Try
            Dim ret As String = ""
            ret = CreateUDF("@GSTCode", "Percentage", "Percentage", SAPbobsCOM.BoFieldTypes.db_Numeric, SAPbobsCOM.BoFldSubTypes.st_Percentage, 5)
            If ret = "" Then
                ret = CreateUDF("@GSTCode", "Description", "Description", SAPbobsCOM.BoFieldTypes.db_Memo, SAPbobsCOM.BoFldSubTypes.st_None, 500)
            End If
            Return ret
        Catch ex As Exception
            Return ex.ToString
        End Try
    End Function
    Private Function CreateUDF(ByVal tableName As String, ByVal fieldName As String, ByVal desc As String, _
                               ByVal fieldType As SAPbobsCOM.BoFieldTypes, ByVal subType As SAPbobsCOM.BoFldSubTypes, _
                               ByVal editSize As Integer) As String
        Try
            Dim fieldID As Long = -1
            'Dim ds As DataSet = connect.ObjectGetAll_Query_SAP("SELECT FieldID FROM CUFD WHERE TableID = '" + tableName + "' AND AliasID = N'" + fieldName + "'")
            'If ds.Tables.Count > 0 Then
            '    If ds.Tables(0).Rows.Count > 0 Then
            '        fieldID = ds.Tables(0).Rows(0).Item("FieldID")
            '    End If
            'End If

            Dim oUdfMD As SAPbobsCOM.UserFieldsMD = Nothing
            oUdfMD = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserFields)

            GC.Collect()
            oUdfMD.TableName = tableName
            oUdfMD.Name = fieldName
            oUdfMD.Type = fieldType
            oUdfMD.Description = desc
            oUdfMD.SubType = subType
            oUdfMD.EditSize = editSize

            Dim lRetCode As Integer = 0

            If oUdfMD.GetByKey(tableName, fieldID) = True Then
                lRetCode = oUdfMD.Update()
            Else
                lRetCode = oUdfMD.Add()
            End If

            System.Runtime.InteropServices.Marshal.ReleaseComObject(oUdfMD)
            oUdfMD = Nothing
            GC.Collect()

            If lRetCode <> 0 Then
                Dim sErrMsg As String = ""
                PublicVariable.oCompany.GetLastError(lRetCode, sErrMsg)
                Return sErrMsg
            Else
                Return ""
            End If
        Catch ex As Exception
            Return ex.ToString
        End Try
    End Function
    Private Function CreateUDT(ByVal tableName As String, ByVal tableDesc As String, ByVal tableType As SAPbobsCOM.BoUTBTableType) As String

        Dim oUdtMD As SAPbobsCOM.UserTablesMD = Nothing
        Try
            GC.Collect()
            oUdtMD = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserTables)

            If oUdtMD.GetByKey(tableName) = False Then
                oUdtMD.TableName = tableName
                oUdtMD.TableDescription = tableDesc
                oUdtMD.TableType = tableType
                oUdtMD.Add()
            Else
                ' MsgBoxWrapper(Strings.Format("UDT {0} already exits.", tableName))
            End If
            'important:release MD object right after finishing.
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oUdtMD)
            oUdtMD = Nothing
            GC.Collect()
        Catch ex As Exception
        End Try



    End Function


End Class
